//
//  XYTest.m
//  str
//
//  Created by qianfeng on 15/8/7.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "XYTest.h"
#import "XYDog.h"
#import "XYCat.h"
#import "XYStudent.h"
#import "XYPerson.h"

@implementation XYTest
//1.
+ (void)bark{
   //方法一
    XYDog * dog1 = [[XYDog alloc] initWithName:@"jk" andAge:2];
    XYDog * dog2 = [[XYDog alloc] initWithName:@"jkk" andAge:3];
    XYDog * dog3 = [[XYDog alloc] initWithName:@"jjk" andAge:4];
    XYDog * dog4 = [[XYDog alloc] initWithName:@"jkjk" andAge:5];
    XYDog * dog5 = [[XYDog alloc] initWithName:@"jkjk1" andAge:6];
    
    NSArray * dogsArray = [NSArray arrayWithObjects:dog1,dog2,dog3,dog4,dog5, nil];
    for (int i = 0; i < [dogsArray count]; i++) {
        id object =  [dogsArray objectAtIndex:i];
        [object bark];
    }
    //方法二
//    NSMutableArray * mdogsArray = [NSMutableArray array];
//    for (int i = 0 ; i < 5; i++) {
//        XYDog * dog = [[XYDog alloc] initWithName:[NSString stringWithFormat:@"dog%d",i] andAge:arc4random() % 5 + 1];
//        [mdogsArray addObject:dog];
//    }
//    for (XYDog * dog in mdogsArray) {
//        [dog bark];
//    }
}

//2.
+ (void) threeCatsAndthreeDogs{
   
    XYCat * cat1 = [[XYCat alloc] init];
    XYCat * cat2 = [[XYCat alloc] init];
    XYCat * cat3 = [[XYCat alloc] init];
    
    XYDog * dog1 = [[XYDog alloc] initWithName:@"jk" andAge:2];
    XYDog * dog2 = [[XYDog alloc] initWithName:@"jkk" andAge:3];
    XYDog * dog3 = [[XYDog alloc] initWithName:@"jjk" andAge:4];
    
    NSMutableArray * Array = [[NSMutableArray alloc] initWithObjects:cat1,dog2,cat2,dog1,cat3,dog3, nil];
    for (id  object in Array) {
        if ([object isKindOfClass:[XYDog class]]) {
            XYDog * dog = (XYDog *)object;
            [dog PrintDogVoice];
        }
        if ([object isKindOfClass:[XYCat class]]) {
            XYCat * cat = (XYCat *)object;
            [cat PrintCatWash];
        }
    }
}


//3.@“welcom to qianfeng" 获取这句话单词的个数
+(void)GetWordsCount:(NSString *)str{
    NSMutableArray * array = [[NSMutableArray alloc] initWithArray:[str componentsSeparatedByString:@" "]];
    for (int i = 0; i < [array count]; i++) {
        NSString * str1 = [array objectAtIndex:i];
        if ([str1 length] == 0) {
            [array removeObjectAtIndex:i];
            i--;
        }
    }
    NSLog(@"%lu",[array count]);
   }

//4.@“Yes i am a so bad man”按照单词逆序
+(void)niXu:(NSString *)str{
    NSMutableArray * array = [[NSMutableArray alloc] initWithArray:[str componentsSeparatedByString:@" "]];
    NSUInteger i ;
    for (i = 0; i < [array count]; i++) {
        NSString * str1 = [array objectAtIndex:i];
        if ([str1 length] == 0) {
            [array removeObjectAtIndex:i];
            i--;
        }
    }
    NSMutableString * mstr = [[NSMutableString alloc] init];
    for ( int i = (int)[array count] - 1; i >= 0; i--) {
        NSString * tempstr = [NSString stringWithFormat:@"%@ ",[array objectAtIndex:i]];
            [mstr appendString:tempstr];
        }
        NSLog(@"%@",mstr);
}

//5.有两个字符串@"This is a boy";@"really  really too  bad”; 将这两个字符串单词,交叠,形成新的字符串 @"This really is really a too boy bad"
+(void)exchangeA:(NSString *)str1 andB:(NSString *)str2{
    NSMutableArray * mstr1 = [[NSMutableArray alloc] initWithArray:[str1 componentsSeparatedByString:@" "]];
    NSMutableArray * mstr2 = [[NSMutableArray alloc] initWithArray:[str2 componentsSeparatedByString:@" "]];
    for (int i = 0; i < [mstr1 count]; i++) {
        NSString * str11 = [NSString stringWithFormat:@"%@",[mstr1 objectAtIndex:i]];
        if ([str11 length] == 0) {
            [mstr1 removeObjectAtIndex:i];
            i--;
        }
    }
    for (int i = 0; i < [mstr2 count]; i++) {
        NSString * strt = [NSString stringWithFormat:@"%@",[mstr2 objectAtIndex:i]];
        if ([strt length] == 0) {
            [mstr2 removeObjectAtIndex:i];
            i--;
        }
    }
    int i;
    NSMutableString * strstr = [[NSMutableString alloc] init];
    for ( i = 0; i < [mstr1 count]; i++) {
        [strstr appendFormat:@"%@%c%@ ",[mstr1 objectAtIndex:i],' ',[mstr2 objectAtIndex:i]];
    }
    for (int j = i; j < [mstr2 count]; j++) {
        [strstr appendFormat:@"%@ ",[mstr2 objectAtIndex:j]];
    }
     NSLog(@"%@",strstr);
    
}


//7.创建一个学生类，学生类有成员变量姓名，年龄，成绩，分别有三个成员变量的set 和get 方法，一个数组中有10个学生对象，要求分别用姓名，年龄，成绩对这10个学生排序

+(void)sortStudent{
    NSMutableArray * StudentArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 10 ; i++) {
        XYStudent * students = [[XYStudent alloc] initWithName:[NSString stringWithFormat:@"stu%d",arc4random() % 10] andAge:arc4random() % 20 + 10 andScore:arc4random() % 40 + 60];
        [StudentArray addObject:students];
    }
    //按姓名排序
//    for (int i = 0; i < [StudentArray count] - 1; i++) {        
//        for (int j = i + 1; j < [StudentArray count]; j++) {
//            XYStudent * stu1 = [StudentArray objectAtIndex:i];
//            XYStudent * stu2 = [StudentArray objectAtIndex:j];
//            if ([stu1 getName] > [stu2 getName]) {
//                [StudentArray exchangeObjectAtIndex:i withObjectAtIndex:j];
//            }
//        }
//    }
    //按年龄排序
//    for (int i = 0; i < [StudentArray count] - 1; i++) {
//        for (int j = i + 1; j < [StudentArray count]; j++) {
//            XYStudent * stu1 = [StudentArray objectAtIndex:i];
//            XYStudent * stu2 = [StudentArray objectAtIndex:j];
//            if ([stu1 getAge] > [stu2 getAge]) {
//                [StudentArray exchangeObjectAtIndex:i withObjectAtIndex:j];
//            }
//        }
//    }
    //按分数排序
    for (int i = 0 ; i < [StudentArray count] - 1; i++) {
        for (int j = i + 1; j < [StudentArray count]; j++) {
            XYStudent * stu1 = [StudentArray objectAtIndex:i];
            XYStudent * stu2 = [StudentArray objectAtIndex:j];
            if ([stu1 getScore] < [stu2 getScore]) {
                [StudentArray exchangeObjectAtIndex:i withObjectAtIndex:j];
            }
        }
    }
    
    for (XYStudent * stu in StudentArray) {
        NSLog(@"%@",stu);
    }
}
//8：创建一个联系人的类成员变量有 姓名，地址，公司，职业，手机，电话
//将文件中的联系人信息解析出来封装到联系人的对象中，将联系人的对象保存到一个可变数组中
//+(void)analysisInformation{
//    XYPerson * person = [[XYPerson alloc] init];
//    NSMutableArray * personArray = [NSMutableArray arrayWithObject:person];
//    
//    
//    
//    NSString * per = [NSString stringWithContentsOfFile:@"/Users/qianfeng/Desktop/qfile.txt" usedEncoding:NSUTF8StringEncoding error:nil];
//
//    
//
//
//
//
//}

@end
